﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculatorFlexibe.Interfaces;

namespace CalculatorFlexibe.Implementations
{
    class GetLROperands : IGetOperands
    {
        public string GetLeftOperand(int position, string operation, string expr)
        {
            
            int current_position = position - 1;
            while ((Char.IsDigit(expr[current_position]) || expr[current_position] == ',') && current_position != 0)
                current_position--;
            if (expr[current_position] != '+' && expr[current_position] != '-' && current_position != 0)
                current_position++;
            return expr.Substring(current_position, position - current_position);
        }

        public string GetRightOperand(int position, string operation, string expr)
        {
            int current_position = position + operation.Length;
            if (expr[current_position] == '+' || expr[current_position] == '-') current_position++;

            while ((Char.IsDigit(expr[current_position]) || expr[current_position] == ',') && current_position != expr.Length - 1)
                current_position++;
            if (current_position != expr.Length - 1) current_position--;
            return expr.Substring(position + operation.Length, current_position - position - operation.Length + 1);
        }
    }
}
